package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcPhoneZoneMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcPhoneZone;
import com.csun.cmny.provider.model.dto.phone.PhoneZoneBindDto;
import com.csun.cmny.provider.model.vo.DmcPhoneZoneVo;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.RoomBuildingVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcPhoneZoneService;
import com.csun.cmny.provider.service.OmcRpcService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-08 17:01
 * @Description:
 */
@Service
public class DmcPhoneZoneServiceImpl extends BaseService<DmcPhoneZone> implements DmcPhoneZoneService {

    @Resource
    private DmcPhoneZoneMapper phoneZoneDao;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcRpcService omcRpcService;

    /**
     * add phone zone.
     *
     * @param dmcPhoneZone the dmc phone zone
     * @param loginAuthDto the login auth dto
     * @return the int
     */
    @Override
    public Integer addPhoneZone(DmcPhoneZone dmcPhoneZone, LoginAuthDto loginAuthDto) {

        // 判断设备是否已经存在相同的防区编号
        DmcPhoneZone dmcPhoneZoneQuery = new DmcPhoneZone();
        dmcPhoneZoneQuery.setZoneNo(dmcPhoneZone.getZoneNo());
        dmcPhoneZoneQuery.setDeviceId(dmcPhoneZone.getDeviceId());
        dmcPhoneZoneQuery = mapper.selectOne(dmcPhoneZoneQuery);
        if (PublicUtil.isNotEmpty(dmcPhoneZoneQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610003, dmcPhoneZoneQuery.getDeviceId(), dmcPhoneZone.getZoneNo());
        }

        // 添加防区信息
        dmcPhoneZone.setUpdateInfo(loginAuthDto);
        Integer result = phoneZoneDao.insertSelective(dmcPhoneZone);

        return result;
    }

    /**
     * edit phone zone.
     *
     * @param dmcPhoneZone the dmc phone zone
     * @param loginAuthDto the login auth dto
     * @return the int
     */
    @Override
    public Integer editPhoneZone(DmcPhoneZone dmcPhoneZone, LoginAuthDto loginAuthDto) {

        Long id = dmcPhoneZone.getId();
        Preconditions.checkArgument(id != null, "防区Id不能为空");

        DmcPhoneZone dmcPhoneZoneQuery = mapper.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(dmcPhoneZoneQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610004, id);
        }

        // 校验防区编号是否重复
        String zoneNo = dmcPhoneZone.getZoneNo();
        if (PublicUtil.isNotEmpty(zoneNo)) {
            dmcPhoneZoneQuery = new DmcPhoneZone();
            dmcPhoneZoneQuery.setId(id);
            dmcPhoneZoneQuery.setZoneNo(zoneNo);

            dmcPhoneZoneQuery = mapper.selectOne(dmcPhoneZoneQuery);
            if (dmcPhoneZoneQuery != null && !dmcPhoneZoneQuery.getId().equals(id)) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610003,
                        dmcPhoneZoneQuery.getDeviceId(), dmcPhoneZoneQuery.getZoneNo());
            }
        }

        dmcPhoneZone.setDeviceId(dmcPhoneZoneQuery.getDeviceId());
        dmcPhoneZone.setUpdateInfo(loginAuthDto);
        Integer result = phoneZoneDao.updateByPrimaryKeySelective(dmcPhoneZone);

        return result;
    }

    /**
     * delete phone zone by id
     *
     * @param id the id
     * @return the int
     */
    @Override
    public Integer deletePhoneZoneById(Long id) {

        Preconditions.checkArgument(id != null, "防区Id不能为空");
        Integer result = phoneZoneDao.deleteByPrimaryKey(id);

        return result;
    }

    /**
     * delete phone zone by device id
     *
     * @param deviceId device id
     * @return the int
     */
    @Override
    public Integer deletePhoneZoneByDeviceId(Long deviceId) {

        Preconditions.checkArgument(deviceId != null, "设备Id不能为空");

        DmcPhoneZone dmcPhoneZone = new DmcPhoneZone();
        dmcPhoneZone.setDeviceId(deviceId);

        return phoneZoneDao.delete(dmcPhoneZone);
    }

    /**
     * query phone zone list by device id
     *
     * @param deviceId the device id
     *
     * @return the phone zone list by device id
     */
    @Override
    public List<DmcPhoneZoneVo> queryPhoneZoneList(Long deviceId) {

        return phoneZoneDao.queryPhoneZoneList(deviceId);
    }

    /**
     * bind phone zone with location
     *
     * @param phoneZoneBindDto the phone zone bind dto
     * @param loginAuthDto     the login auth dto
     * @return the int.
     */
    @Override
    public Integer bind(PhoneZoneBindDto phoneZoneBindDto, LoginAuthDto loginAuthDto) {

        Long id = phoneZoneBindDto.getId();
        Integer bindType = phoneZoneBindDto.getBindType();

        // 1.校验防区是否存在
        DmcPhoneZone dmcPhoneZoneQuery = phoneZoneDao.selectByPrimaryKey(id);
        if (PublicUtil.isEmpty(dmcPhoneZoneQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC10069002);
        }

        // 2.校验防区是否已经绑定
        if (PublicUtil.isNotEmpty(dmcPhoneZoneQuery.getBindType())) {
            throw new DmcBizException(ErrorCodeEnum.DMC10069001);
        }

        // 3.校验防区绑定类型
        Preconditions.checkArgument(!PubUtils.isNull(bindType),
                cmnyMessageSource.getMessage("phone.zone.bind.type.null", null));

        Preconditions.checkArgument(bindType >= 0 && bindType <= 3,
                cmnyMessageSource.getMessage("phone.zone.bind.type.range.error", null));

        String location = phoneZoneBindDto.getLocation();
        switch (bindType) {
            case 0 :    // 自定义 - 位置不能为空
                Preconditions.checkArgument(PublicUtil.isNotEmpty(location),
                        cmnyMessageSource.getMessage("phone.zone.bind.location.null", null));
                break;
            case 1 :    // 4.校验绑定的老人
                Preconditions.checkArgument(!PubUtils.isNull(phoneZoneBindDto.getOldmanId()),
                        cmnyMessageSource.getMessage("phone.zone.bind.oldman.id.null", null));

//                Preconditions.checkArgument(!PubUtils.isNull(phoneZoneBindDto.getOldmanName()),
//                        cmnyMessageSource.getMessage("phone.zone.bind.oldman.name.null", null));
                // TODO 校验老人Id是否存在
                OldmanBedRoomBuildingVo oldmanBedRoomBuildingVo = omcRpcService
                        .queryOldmanBedRoomBuildingByOldmanId(phoneZoneBindDto.getOldmanId());
                if (PublicUtil.isEmpty(oldmanBedRoomBuildingVo)) {
                    // 老人不存在
                    throw new DmcBizException(ErrorCodeEnum.DMC100610062);
                }

                phoneZoneBindDto.setOldmanName(oldmanBedRoomBuildingVo.getOldmanName());
                break;
            case 2 :    // 5.校验绑定床位
                Preconditions.checkArgument(!PubUtils.isNull(phoneZoneBindDto.getBedId()),
                        cmnyMessageSource.getMessage("phone.zone.bind.bed.id.null", null));
                // 校验床位Id是否存在
                // TODO 校验床位Id是否存在
                OldmanBedRoomBuildingVo bedRoomBuildingVo = omcRpcService
                        .queryOldmanBedRoomBuildingByBedId(phoneZoneBindDto.getBedId());
                if (PublicUtil.isEmpty(bedRoomBuildingVo)) {
                    // 床位不存在
                    throw new DmcBizException(ErrorCodeEnum.DMC100610060);
                }

                location = bedRoomBuildingVo.getLocation();
                break;
            case 3 :    // 6.校验绑定的房间
                Preconditions.checkArgument(!PubUtils.isNull(phoneZoneBindDto.getRoomId()),
                        cmnyMessageSource.getMessage("phone.zone.bind.room.id.null", null));
                // 校验房间Id是否存在
                // TODO 校验房间Id是否存在
                RoomBuildingVo roomBuildingVo = omcRpcService
                        .queryRoomBuildingByRoomId(phoneZoneBindDto.getRoomId());
                if (PublicUtil.isEmpty(roomBuildingVo)) {
                    // 房间不存在
                    throw new DmcBizException(ErrorCodeEnum.DMC100610058);
                }

                location = roomBuildingVo.getLocation();
                break;
        }
        phoneZoneBindDto.setLocation(location);

        DmcPhoneZone dmcPhoneZone = new DmcPhoneZone();
        BeanUtils.copyProperties(phoneZoneBindDto, dmcPhoneZone);
        dmcPhoneZone.setUpdateInfo(loginAuthDto);

        return phoneZoneDao.updateByPrimaryKeySelective(dmcPhoneZone);
    }

    /**
     * untied phone zone with location
     *
     * @param id the zone id
     *
     * @return the int.
     */
    @Override
    public Integer zoneUntied(Long id) {

        return phoneZoneDao.zoneUntied(id);
    }
}
